#include "Emitter.h"


Emitter::Emitter(void)
{
	StartPos_X = 0;
	StartPos_Y = 0;
	StartPos_Z = 0;

	StopPos_X = 0;
	StopPos_Y = 0;
	StopPos_Z = 0;

	StartVel_X = 0;
	StartVel_Y = 0;
	StartVel_Z = 0;

	StopVel_X = 0;
	StopVel_Y = 0;
	StopVel_Z = 0;

	StartScale_X = 1;
	StartScale_Y = 1;
	StartScale_Z = 1;

	StopScale_X = 1;
	StopScale_Y = 1;
	StopScale_Z = 1;

	Pos_X = 0;
	Pos_Y = 0;
	Pos_Z = 0;

	Alpha_start = 255;
	Alpha_finish = 255;

	vPointBased = true;
	vTimeBased = false;
	vCircleBased = false;

	Lifetime = 5;
}


Emitter::~Emitter(void)
{
}

void Emitter::Update(float elapsedTime)
{
	if(dead_part.size() > 0 && live_part.size() < NumParticles)
	{
		while(live_part.size() != NumParticles)// && DelayTimer <= 0)
		{
			// calculate random offsets
			if(StopPos_X == 0 && StopPos_X == 0)
				Offset_X = 0;
			else if(StartPos_X > StopPos_X)
			{
				Offset_X = (float)(rand() % (int)StopPos_X + (int)StartPos_X);
			}
			else
			{
				Offset_X = (float)(rand() % (int)StartPos_X + (int)StopPos_X);
			}

			if(StopPos_Y == 0 && StopPos_Y == 0)
				Offset_Y = 0;
			else if(StartPos_Y > StopPos_Y)
			{
				Offset_Y = (float)(rand() % (int)StopPos_Y + (int)StartPos_Y);
			}
			else
			{
				Offset_Y = (float)(rand() % (int)StartPos_Y + (int)StopPos_Y);
			}

			if(StopPos_Z == 0 && StopPos_Z == 0)
				Offset_Z = 0;
			else if(StartPos_Z > StopPos_Z)
			{
				Offset_Z = (float)(rand() % (int)StopPos_Z + (int)StartPos_Z);
			}
			else
			{
				Offset_Z = (float)(rand() % (int)StartPos_Z + (int)StopPos_Z);
			}

			Particle* temp = dead_part.front();
			dead_part.pop();
			ParticleReset(*temp);

			if(vTimeBased)
			{
				temp->PosX = Offset_X;
				temp->PosY = Offset_Y;
				temp->PosZ = Offset_Z;
			}
			else if(vPointBased)
			{
				temp->VelX = Offset_X;
				temp->VelY = Offset_Y;
				temp->VelZ = Offset_Z;
			}

			live_part.push_back(temp);
		}

		/*
			DelayTimer -= elapsedTime;

                if (DelayTimer < 0)
                    DelayTimer = particledelay;
		*/
	}

	for(unsigned int i = 0; i < live_part.size(); ++i)
	{
		live_part[i]->Update(elapsedTime, vTimeBased, vCircleBased);

		if(live_part[i]->LifeTime <= 0)
		{
			Particle* temp = live_part[i];
			dead_part.push(temp);
			live_part.pop_back();
		}
	}

}

// Resets the particle after dying
void Emitter::ParticleReset( Particle particle )
{
	particle.LifeTime = Lifetime;
	//particle.Scale = StartScale_X;

	particle.VelX = StartVel_X;
	particle.VelY = StartVel_Y;
	particle.VelZ = StartVel_Z;

	particle.PosX = Pos_X;
	particle.PosY = Pos_Y;
	particle.PosZ = Pos_Z;
}

void Emitter::StartEmitter( void )
{
	for(unsigned int i = 0; i != NumParticles; i++)
	{
		Particle* temp = new Particle(this);
		ParticleReset(*temp);
		dead_part.push(temp);
	}
}

void Emitter::Render( void )
{
	for(unsigned int i = 0; i < live_part.size(); ++i)
	{
		live_part[i]->Render();
	}
}


